package com.snail.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snail.model.dto.user.DeptQueryDTO;
import com.snail.model.dto.user.DeptSaveDTO;
import com.snail.model.pojo.user.SysDept;
import com.snail.model.vo.user.DeptVO;
import com.snail.result.Result;
import com.snail.service.SysDeptService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@RequiredArgsConstructor
public class DeptController {
    
    private final SysDeptService deptService;
    
//    @Operation(summary = "新增部门")
    @PostMapping("/add")
    public Result addDept(@RequestBody DeptSaveDTO deptSaveDTO) {
      return   deptService.addDept(deptSaveDTO);
    }

    @PostMapping("/update")
    public Result<Void> updateDept(@RequestBody DeptSaveDTO deptSaveDTO) {
        return deptService.updateDept(deptSaveDTO);
    }
    
//    @Operation(summary = "删除部门(逻辑删除)")
    @DeleteMapping("/{id}")
    public Result<Void> deleteDept(
            @Parameter(description = "部门ID") @PathVariable Long id) {
        return deptService.deleteDept(id);
    }
    
//    @Operation(summary = "分页查询部门")
    @PostMapping("/find")
    public Result<IPage<DeptVO>> queryDeptPage(@RequestBody DeptQueryDTO queryDTO) {
        Page<SysDept> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        IPage<DeptVO> result = deptService.queryDeptPage(page, queryDTO);
        return Result.success(result);
    }
    
//    @Operation(summary = "批量删除部门")
    @DeleteMapping("/batch")
    public Result<Void> deleteDeptBatch(@RequestBody List<Long> ids) {
        return deptService.deleteDeptBatch(ids);
    }
    
//    @Operation(summary = "根据用户ID修改部门")
    @PostMapping("/user/{userId}")
    public Result<Void> updateDeptByUserId(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @RequestBody DeptSaveDTO deptSaveDTO) {
        return deptService.updateDeptByUserId(userId, deptSaveDTO);
    }

//    @Operation(summary = "根据用户ID查询部门")
    @GetMapping("getDeptByUserId")
    public Result findDeptByUserid(){
        return deptService.getDeptByUserId();
    }


    /**
     * 获取部门详情包含评论信息
     * @param id
     * @return
     */
    @GetMapping("/{id}/detail")
    public Result<DeptVO> getDeptDetail(@PathVariable Long id) {
        DeptVO deptVO = deptService.getDeptDetail(id);
        return Result.success(deptVO);
    }
}